/**
 * Application class that is used for initializing the preloader, setting current progress and creates all object declared inside the application
 * @type {omplusClass}
 */
Omplus.application.Application = Omplus.base.extend(Omplus.base.Base, {
    omplusType : "Application",
    init : function(){},

    body : {
        omplusName:"startBody"
    },

    languages : null,

    users : null,

    loader : {
        omplusName:"startLoader"
    },

    create : function(){
        var objects = Omplus.globals.getNotCreated();
        var helper = ["Controller", "Proxy", "Element", "Component"];
        var number = Omplus.globals.getNotCreatedLength();
        if (this.languages){
            number++;
        }
        if (this.users){
            number++;
        }
        var ompluses = 0;
        var percent;
        for(var j in helper)
            for (var i in objects[helper[j]]){
                Omplus.base.create(objects[helper[j]][i].className, objects[helper[j]][i].properties);
                ompluses++;
                percent = Math.ceil(ompluses / number * 100);
                this.step(percent);
            }
        if (this.languages){
            var helper = this;
            Omplus.globals.getObject(this.languages.omplusName).getAction(this.languages.action).callback = function(){
                ompluses++;
                percent = Math.ceil(ompluses / number * 100);
                helper.step(percent);
                if (helper.users){
                    helper.checkUser(ompluses, number);
                }
            }
            Omplus.globals.getObject(this.languages.omplusName).getAction(this.languages.action).run();
        }
        else if (this.users) this.checkUser(ompluses, number);
        else this.step(100);
    },

    checkUser : function(ompluses, number){
        var helper = this;
        Omplus.globals.getObject(this.users.omplusName).getAction(this.users.action).callback = function(){
            helper.step(100);
            Omplus.globals.getObject(helper.loader.omplusName).hide();
        }
        Omplus.globals.getObject(this.users.omplusName).getAction(this.users.action).run();
    },

    step : function(value){},

    run : function(){
        var helper = this;
        this.loader.onRenderFinish = function () {
            helper.create();
        }
        this.loader.renderTo = this.body.omplusName;
        Omplus.base.create(Omplus.views.Body, this.body);
        Omplus.base.create(Omplus.views.Panel, this.loader).show();
    }
});